package org.vectrics.mail;

import java.util.Locale;
import org.apache.ecs.html.Body;
import org.apache.ecs.html.Font;
import org.apache.ecs.html.Head;
import org.apache.ecs.html.Html;
import org.apache.log4j.Logger;
import org.vectrics.SystemException;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Table;
import org.vectrics.domain.Domain;
import org.vectrics.provider.CoreServiceLocator;

public abstract class EmailHtmlBase {
	protected Logger log = Logger.getLogger(this.getClass());
	private Locale locale = null;
	private Domain domain = null;
	private EmailTemplate emailTemplate = null;
	
	public EmailHtmlBase(Domain domain, Locale locale) {
		super();
		if (locale == null)
			throw new SystemException("Locale not given");
		if (domain == null)
			throw new SystemException("Domain not given");
		this.domain = domain;
		this.locale = locale;
	}

	public EmailTemplate getEmailTemplate() {
		if (emailTemplate == null) {
			emailTemplate = createEmailTemplate();
			emailTemplate.setLocale(this.locale);
			emailTemplate.setDomain(this.domain);
			emailTemplate.render();
			this.renderContent(emailTemplate.getContentCell());
		}
		return(emailTemplate);
	}
	
	abstract protected EmailTemplate createEmailTemplate();
	
	abstract protected void renderContent(Cell cell);
	
	protected Domain getDomain() {
		return(this.domain);
	}
	
	public Head getHead() {
		return(this.getEmailTemplate().getHead());
	}

	/**
	 * @return Returns the html.
	 */
	public Html renderHtml() {
		return(this.getEmailTemplate().getHtml());
	}

	public String getPhrase(String code) {
		String phrase = CoreServiceLocator.getPhraseProvider().getMessage(code, 
				this.getLocale());
		return(phrase);
	}

/*
	public Html render(Cell cell) {
		Cell contentCell = this.getEmailTemplate().getContentCell();
		this.renderContent(contentCell);

		if (log.isDebugEnabled())
			log.debug("Email HTML:\r\n" + this.getEmailTemplate().getHtml());
		
		return(this.getEmailTemplate().getHtml());
	}
	*/
	
	protected void appendPropertyRow(String caption, String value, Table table) {
		EmailTable.appendPropertyRow(caption, value, table);
	}

	public static Font createSmallFont() {
		Font font = new Font();
		font.setStyle("font:normal 0.7em Verdana");
		return(font);
	}

	public static Font createBoldSmallFont() {
		Font font = new Font();
		font.setStyle("font:bold 0.7em Verdana");
		return(font);
	}

	/**
	 * @return Returns the locale.
	 */
	public Locale getLocale() {
		return locale;
	}

	/**
	 * @param locale The locale to set.
	 */
	public void setLocale(Locale locale) {
		this.locale = locale;
	}

	
}
